<template>
  <div
    class="carouslitem-container"
    ref="imgContainerRef"
    @mousemove="handlerMousemove"
    @mouseleave="handlerMouseLeave"
  >
    <div class="img-container" ref="imageInsideRef" :style="computedLeftTop">
      <ImageLoader
        :src="item.bigImg"
        :placeholder="item.midImg"
        @loaded="handlerShowWords"
      />
    </div>

    <p class="title" ref="title">{{ item.title }}</p>
    <p class="desc" ref="desc">{{ item.description }}</p>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader";
export default {
  name: "CarousIitem",
  props: {
    item: {
      type: Object,
      default: () => {},
    },
  },
  components: {
    ImageLoader,
  },
  data() {
    return {
      titleWidth: 0,
      descWidth: 0,

      imgContainerW: 0,
      imgContainerH: 0,
      imgInsideW: 0,
      imgInsideH: 0,

      mouseX: 0,
      mouseY: 0,
    };
  },
  computed: {
    computedLeftTop() {
      const extraW = this.imgInsideW - this.imgContainerW;
      const extraH = this.imgInsideH - this.imgContainerH;
      const left = -(extraW / this.imgContainerW) * this.mouseX;
      const top = -(extraH / this.imgContainerH) * this.mouseY;
      return {
        transform: `translate(${left}px, ${top}px)`,
      };
    },
    center() {
      return {
        x: this.$refs.imgContainerW / 2,
        y: this.$refs.imgContainerH / 2,
      };
    },
  },
  mounted() {
    this.titleWidth = this.$refs.title.clientWidth;
    this.descWidth = this.$refs.desc.clientWidth;
    this.getImageContainerSize();
    this.mouseX = this.center.x;
    this.mouseY = this.center.y;
    window.addEventListener("resize", this.getImageContainerSize);
  },
  destroyed() {
    window.removeEventListener("resize", this.getImageContainerSize);
  },
  methods: {
    handlerMousemove(e) {
      const { left, top } = this.$refs.imgContainerRef.getBoundingClientRect();
      this.mouseX = e.clientX - left;
      this.mouseY = e.clientY - top;
    },
    handlerMouseLeave() {
      this.mouseX = this.center.x;
      this.mouseY = this.center.y;
    },
    handlerShowWords() {
      this.$refs.title.style.width = 0;
      this.$refs.title.style.opacity = 1;
      this.$refs.title.clientWidth;
      this.$refs.title.style.transition = "5s";
      this.$refs.title.style.width = this.titleWidth + "px";

      this.$refs.desc.style.width = 0;
      this.$refs.desc.style.opacity = 1;
      this.$refs.desc.clientWidth;
      this.$refs.desc.style.transition = "5s 3s";
      this.$refs.desc.style.width = this.descWidth + "px";
    },
    getImageContainerSize() {
      this.imgContainerW = this.$refs.imgContainerRef.clientWidth;
      this.imgContainerH = this.$refs.imgContainerRef.clientHeight;
      this.imgInsideW = this.$refs.imageInsideRef.clientWidth;
      this.imgInsideH = this.$refs.imageInsideRef.clientHeight;
    },
  },
};
</script>

<style scoped lang="less">
.carouslitem-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .img-container {
    width: 110%;
    height: 110%;
    overflow: hidden;

    transition: 300ms ease-in-out;
  }

  .title,
  .desc {
    position: absolute;
    white-space: nowrap;
    overflow: hidden;
    left: 35px;
    top: 70%;
    color: #fff;
    z-index: 999;
    opacity: 0;
    letter-spacing: 8px;
    text-shadow: 0 2px 18px #000;
  }
  .title {
    font-size: 1.6em;
    transform: translateY(0);
  }
  .desc {
    transform: translateY(58px);
  }
}
</style>
